/**
 * 
 */
package cn.gov.msa.controller;

import java.io.ByteArrayOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.gov.msa.util.FileUtil;
import cn.gov.msa.util.PropertiesTools;
import cn.gov.msa.util.WebServiceTools;
import cn.msa.gov.model.UserDetail;
import net.sf.json.JSONObject;

/**   
* @Title: BusinessControll.java 
* @Package cn.gov.msa.controller 
* @Description: TODO(用一句话描述该文件做什么) 
* @author 何宇江  
* @date 2018年2月21日 上午11:34:09 
*/


@RestController
@RequestMapping(value = "/business", produces = "text/plain;charset=UTF-8")
public class BusinessControll {

	private static String wsUrl  = PropertiesTools.readProperties("gahWS.url");
 
    String methods = PropertiesTools.readProperties("gahWS.methods");

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public String getList(@RequestBody JSONObject param, HttpServletRequest httpRequest) throws Exception {
		UserDetail userDetail = (UserDetail)httpRequest.getSession().getAttribute("user");
		String companyId = userDetail.getCompanyId();
		if(companyId == null || "".equals(companyId)){
			return "{\"total\":0,\"rows\":\"[]\"}";
		}
		// 查询当前登录用户所属公司的申报记录
		param.put("companyid", companyId);
        String[] parameter = new String[]{"DKS100", param.toString()};       
        String result = WebServiceTools.getDatasByUrl(wsUrl, methods, parameter);

        JSONObject jsonResult = JSONObject.fromObject(result);
        if(1 == jsonResult.getInt("result")){
        	result = jsonResult.getString("datas");
        }
        else{
        	JSONObject json = new JSONObject();
        	json.put("total", 0);
        	json.put("rows", "[]");
        	result = json.toString();
        }
        
        return result;  
    } 
	
	
	@RequestMapping(value = "/apply", method = RequestMethod.POST)
	public String apply(@RequestBody JSONObject param, HttpSession session, HttpServletRequest httpRequest) throws Exception {
		String result = "{\"result\":0,\"info\":\"还未登录！\"}";
		JSONObject userinfo = new JSONObject();
		try{
			userinfo = JSONObject.fromObject(session.getAttribute("user"));
		}
		catch(Exception e){
			result = "{\"result\":0,\"info\":\"还未登录！\"}";
		}
		
		if(!userinfo.isNullObject() && !userinfo.isEmpty()){
			JSONObject jsonUser = new JSONObject();
			try {
				jsonUser.put("company", userinfo.getString("company"));
				jsonUser.put("companyid", userinfo.getString("companyId"));
				jsonUser.put("contactaddr", userinfo.optString("contactAddr"));
				jsonUser.put("email", userinfo.optString("email"));
				jsonUser.put("mobilephone", userinfo.getString("mobilePhone"));
				jsonUser.put("proposer", userinfo.getString("proposer"));
				jsonUser.put("proposerid", userinfo.getString("proposerId"));
				jsonUser.put("telephone", userinfo.optString("telePhone"));
			} catch (Exception e) {
				return "{\"result\":0,\"info\":\"您的账号公司信息不全，无法申报业务，请到相关部门申请补全公司信息。\"}";
			}
			
			param.put("user", jsonUser);
			
	        String[] parameter = new String[]{"DKU101", param.toString()};
	        
	        result = WebServiceTools.getDatasByUrl(wsUrl, methods, parameter);
		}
    	
        return result;  
    } 
	
	@RequestMapping(value = "/getMaterial", method = RequestMethod.GET)
	public String getMaterial(String itemno) throws Exception {
		JSONObject param = new JSONObject();
		param.put("itemno", itemno);
		
        String[] parameter = new String[]{"DKS102", param.toString()};
        
        String result = WebServiceTools.getDatasByUrl(wsUrl, methods, parameter);
           	
        return result;  
    } 

	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public String delete(String applyid) throws Exception {
		JSONObject param = new JSONObject();
		param.put("applyid", applyid);
		
        String[] parameter = new String[]{"DKU604", param.toString()};
        
        String result = WebServiceTools.getDatasByUrl(wsUrl, methods, parameter);
           	
        return result;  
    } 
	

	@RequestMapping(value = "/getapplyinfo", method = RequestMethod.GET)
	public String getApplyInfo(String applyno, HttpServletRequest httpRequest) throws Exception {
		JSONObject param = new JSONObject();
		param.put("applyno", applyno);
		
        String[] parameter = new String[]{"DKS206", param.toString()};
        
        String result = WebServiceTools.getDatasByUrl(wsUrl, methods, parameter);

        JSONObject jsonResult = JSONObject.fromObject(result);
    	
        return result;  
    } 


	@RequestMapping(value = "/getcorrectioninfo", method = RequestMethod.GET)
	public String getCorrectionInfo(String applyno) throws Exception {
		String result = "";
		
		JSONObject param = new JSONObject();
		param.put("applyno", applyno);
			
        String[] parameter = new String[]{"DKS108", param.toString()};
	       
	    result = WebServiceTools.getDatasByUrl(wsUrl, methods, parameter);
    	
        return result; 
    } 
	

	@RequestMapping(value = "/getamanuensis", method = RequestMethod.GET)
	public String getAmanuensis(String applyid) throws Exception {
		String result = "";
		
		JSONObject param = new JSONObject();
		param.put("applyid", applyid);
			
        String[] parameter = new String[]{"DKS124", param.toString()};
	       
	    result = WebServiceTools.getDatasByUrl(wsUrl, methods, parameter);
    	
        return result; 
    } 

	@RequestMapping(value = "/getamanuensisbyid", method = RequestMethod.GET)
	public void getAmanuensisById(String amanuensisid, HttpServletResponse httpServletResponse) throws Exception {
		String result = "";
		
		JSONObject param = new JSONObject();
		param.put("amanuensisid", amanuensisid);
			
        String[] parameter = new String[]{"DKS125", param.toString()};
	       
	    result = WebServiceTools.getDatasByUrl(wsUrl, methods, parameter);
	    
	   
	    JSONObject jsonResult = JSONObject.fromObject(result);
	    
	    if(1 == jsonResult.getInt("result")){
//	    	String base64String = FileUtil.hexStringToBytes(jsonResult.getJSONObject("datas").getString("filecontent"));
	    	
	    	byte[] bytes = FileUtil.hexStringToBytes(jsonResult.getJSONObject("datas").getString("filecontent"));
	    	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	    	baos.write(bytes); //把byte写进输出流里
	    	if(baos!= null){
	    	                httpServletResponse.setContentType("application/pdf");
	    	                httpServletResponse.setContentLength(baos.size());
	    	                httpServletResponse.setHeader("Expires", "0");
	    	                httpServletResponse.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
	    	                httpServletResponse.setHeader("Pragma", "public");
	    					httpServletResponse.setHeader("Content-Disposition", "inline;filename="+ new String((jsonResult.getJSONObject("datas").getString("amanuensisname")+ ".pdf").getBytes(), "iso-8859-1"));
	    	                javax.servlet.ServletOutputStream sos = httpServletResponse.getOutputStream();
	    	                baos.writeTo(sos); //byte输出流写入servlet输出流
	    	                sos.flush();
	    	                sos.close();
	    	                baos.close();
	    	            }
	    }
    	
    } 
	

	@RequestMapping(value = "/savecomment", method = RequestMethod.POST)
	public String saveComment(@RequestBody JSONObject param, HttpSession session, HttpServletRequest httpRequest) throws Exception {
		String result = "";
		JSONObject userinfo = new JSONObject();
		String userid = "";
		try{
			userinfo = JSONObject.fromObject(session.getAttribute("user"));
			userid = userinfo.getString("userId");
		}
		catch(Exception e){
			result = "{\"result\":0,\"info\":\"还未登录！\"}";
		}
		
		if(!"".equals(userid)){
			JSONObject jsonUser = new JSONObject();
			jsonUser.put("company", userinfo.optString("company"));
			jsonUser.put("companyid", userinfo.optString("companyId"));
			jsonUser.put("contactaddr", userinfo.optString("contactAddr"));
			jsonUser.put("email", userinfo.optString("email"));
			jsonUser.put("mobilephone", userinfo.optString("mobilePhone"));
			jsonUser.put("proposer", userinfo.optString("proposer"));
			jsonUser.put("proposerid", userinfo.optString("proposerId"));
			jsonUser.put("telephone", userinfo.optString("telePhone"));
			
//			param.put("user", jsonUser);
			
	        String[] parameter = new String[]{"DKU605", param.toString()};
	        
	        result = WebServiceTools.getDatasByUrl(wsUrl, methods, parameter);
		}
    	
        return result;  
    } 
}
